//
//  UITableView+OADTableConfig.m
//  OASDK_Works
//
//  Created by ChenJie on 3/1/20.
//  Copyright © 2020 Mist. All rights reserved.
//

#import "UITableView+OADTableConfig.h"
#import <objc/runtime.h>

@implementation UITableView (OADTableConfig)

/*
 * Property: oa_tableConfig
 */

- (OADTableConfig *)oa_tableConfig_ {
    OADTableConfig *tableConfig = objc_getAssociatedObject(self, _cmd);
    if (!tableConfig) {
        tableConfig = [OADTableConfig new];
        self.oa_tableConfig_ = tableConfig;
        self.dataSource = self.oa_implementation;
        self.delegate = self.oa_implementation;
    }
    return tableConfig;
}

- (void)oa_setTableConfig_:(OADTableConfig *)oa_tableConfig {
    objc_setAssociatedObject(self, @selector(oa_tableConfig_), oa_tableConfig, OBJC_ASSOCIATION_RETAIN_NONATOMIC) ;
}

/*
 * Property: oa_implementation
 */

- (OADTableImplementation *)oa_implementation {
    OADTableImplementation *implementation = objc_getAssociatedObject(self, _cmd);
    if (!implementation) {
        implementation = [OADTableImplementation new];
        self.oa_implementation = implementation;
    }
    return implementation;
}

- (void)oa_setImplementation:(OADTableImplementation *)oa_implementation {
    objc_setAssociatedObject(self, @selector(oa_implementation), oa_implementation, OBJC_ASSOCIATION_RETAIN_NONATOMIC) ;
}

#pragma mark - Methods

/*
 * UITableViewDataSource
 */

- (NSInteger)oa_numberOfSectionsInTableView {
    return [self.oa_tableConfig_ numberOfSectionsInTableView];
}

- (NSInteger)oa_numberOfRowsInSection:(NSInteger)section {
    return [self.oa_tableConfig_ numberOfRowsInSection:section];
}

- (UITableViewCell *)oa_cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [self.oa_tableConfig_ tableView:self cellForRowAtIndexPath:indexPath];
}

/*
 * UITableViewDelegate
 */

- (CGFloat)oa_heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [self.oa_tableConfig_ heightForRowAtIndexPath:indexPath];
}


@end
